package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.LocaleWorkQueryCountQueryParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.SingleLineView;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2016/12/21 19:02
 */

public class LocaleWorkQueryCountQueryFragment extends BaseFragment
    implements View.OnClickListener {

  /**
   * 返回的
   */
  //public static final String EXTRA_RESULT_START_DATE = "com.isunland.intelligentFarmbyWJ.ui.QueryDateFragment.EXTRA_RESULT_START_DATE";
  //public static final String EXTRA_RESULT_END_DATE = "com.isunland.intelligentFarmbyWJ.ui.QueryDateFragment.EXTRA_RESULT_END_DATE";

  private static final int REQUEST_START_DATE = 1;//开始日期
  private static final int REQUEST_END_DATE = 2;//结束日期
  private static final int REQUEST_ORANIZATION = 3;//选择组织
  private static final int MENU_ITEM_CLEAR = 4;

  @BindView(R.id.tv_startTime_workConvert) SingleLineView mStartDateTv;
  @BindView(R.id.tv_endTime_workConvert) SingleLineView mEndDateTv;
  @BindView(R.id.tv_taskMan) SingleLineView mTvTaskMan;
  @BindView(R.id.tv_equipment) SingleLineView mTvEquipment;
  @BindView(R.id.tv_workOrganization) SingleLineView mTvOrganization;

  private FragmentManager mFm;
  private LocaleWorkQueryCountQueryParams mParams;

  @Override public void initData() {
    super.initData();
    if (mBaseParams != null && mBaseParams instanceof LocaleWorkQueryCountQueryParams) {
      mParams = (LocaleWorkQueryCountQueryParams) mBaseParams;
    }
    if (mParams == null) mParams = new LocaleWorkQueryCountQueryParams();
    mFm = mActivity.getSupportFragmentManager();
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.query);
    //作业地块页面只显示组织
    if (mParams.isOnlyShowOrg()) {
      mStartDateTv.setVisibility(View.GONE);
      mEndDateTv.setVisibility(View.GONE);
      mTvTaskMan.setVisibility(View.GONE);
      mTvEquipment.setVisibility(View.GONE);
    }
    mStartDateTv.setTextContent(MyDateUtil.format(mParams.getStartDate(), MyDateUtil.TYPE_DATE));
    mEndDateTv.setTextContent(MyDateUtil.format(mParams.getEndDate(), MyDateUtil.TYPE_DATE));
    mTvTaskMan.setTextContent(mParams.getMan());
    mTvEquipment.setTextContent(mParams.getEquipment());
    mTvOrganization.setTextContent(mParams.getOrgnazition());

    mStartDateTv.setOnClickContentListener(this);
    mEndDateTv.setOnClickContentListener(this);
    mTvOrganization.setOnClickContentListener(this);
  }

  //初始化界面
  @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_locale_work_query_count_query, container, false);
    ButterKnife.bind(this, v);
    return v;
  }

  @Override public void onClick(View v) {
    if (v == null) return;
    if (!(v.getTag() instanceof Integer)) return;
    int id = (int) v.getTag();

    DialogFragment dialog;
    switch (id) {
      case R.id.tv_startTime_workConvert:
        dialog = BaseYMDTimeDialogFragment.newInstance(mParams.getStartDate());
        dialog.setTargetFragment(this, REQUEST_START_DATE);
        break;
      case R.id.tv_endTime_workConvert:
        dialog = BaseYMDTimeDialogFragment.newInstance(mParams.getEndDate());
        dialog.setTargetFragment(this, REQUEST_END_DATE);
        break;
      case R.id.tv_workOrganization:
        dialog = new SysOrgBelongDialogFragment();
        dialog.setTargetFragment(this, REQUEST_ORANIZATION);
        break;
      default:
        return;
    }

    dialog.show(mFm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_START_DATE && data != null) {
      Date startDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      mStartDateTv.setTextContent(MyDateUtil.format(startDate, MyDateUtil.TYPE_DATE));

      return;
    }
    if (requestCode == REQUEST_END_DATE && data != null) {
      Date endDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      mEndDateTv.setTextContent(MyDateUtil.format(endDate, MyDateUtil.TYPE_DATE));

      return;
    }
    if (requestCode == REQUEST_ORANIZATION && data != null) {
      CustomerDialog value =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mTvOrganization.setTextContent(value.getName());
      mParams.setOrgCode(value.getId());
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    menu.add(0, MENU_ITEM_CLEAR, 0, R.string.clearChoose)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case R.id.menu_item_confirm:
        //if (mParams.getStartDate() == null || mParams.getEndDate() == null) {
        //  ToastUtil.show("请选择日期");
        //  return false;
        //}
        //if (mParams.getStartDate().after(mParams.getEndDate())) {
        //  ToastUtil.show("请选择正确的时间范围");
        //  return false;
        //}

        Intent intent = new Intent();
        mParams.setMan(MyStringUtil.getText(mTvTaskMan.getViewContent()));
        mParams.setEquipment(MyStringUtil.getText(mTvEquipment.getViewContent()));
        mParams.setOrgnazition(MyStringUtil.getText(mTvOrganization.getViewContent()));
        mParams.setStartDate(MyDateUtil.parse(MyStringUtil.getText(mStartDateTv.getViewContent())));
        mParams.setEndDate(MyDateUtil.parse(MyStringUtil.getText(mEndDateTv.getViewContent())));
        intent.putExtra(EXTRA_PARAMS, mParams);
        mActivity.setResult(Activity.RESULT_OK, intent);
        if (!mActivity.isFinishing()) mActivity.finish();
        return true;
      case MENU_ITEM_CLEAR:
        //清空界面
        mEndDateTv.setTextContent("");
        mStartDateTv.setTextContent("");
        mTvTaskMan.setTextContent("");
        mTvOrganization.setTextContent("");
        mTvEquipment.setTextContent("");
        //清空数据
        mParams.setOrgCode("");
        ToastUtil.show(R.string.clearChooseCondition);
        break;
      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }
}